package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.User;
import org.csu.mypetstore.persistence.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    //MD5 加密所用的盐
    private static final String salt="1a2b3c4d";

    public User findUserByUsername(String username){
        return userMapper.findUserByUsername(username);
    }

    public boolean remove(String username)
    {
        userMapper.remove(username);
        return true;
    }
    public boolean saveUser(String username,String password,String firstname,String lastname,String phone,String email)
    {
        return userMapper.saveUser(username,password,firstname,lastname,phone,email);
    }
    public boolean showUsers(String username){
        /*List<User> list = userMapper.showUsers();
        for (User user:list) {
            System.out.println(user.getUsername());
        }*/
        User user = userMapper.showUsers(username);
        System.out.println(user.getUsername());
        return true;
    }
    public boolean updateUserByUsername(User user){
        String inputPassword = user.getPassword();
        System.out.println("*******************************************");
        //System.out.println("输入密码： "+inputPassword+"  MD5加密后： "+MD5Password);
        System.out.println("*******************************************");

        user.setPassword(inputPassword);
        return userMapper.updateUserByUsername(user);
    }

    /**
     * @Description //TODO 登录获取输入密码，使用MD%加密后与数据库中存储密码匹配查询
     * @Date 8:28 下午 2022/3/14
     * @Param [user]
     * @return org.csu.mypetstore.domain.User
     **/
   public User signin(String username,String password){
        System.out.println("*******************************************");
        //System.out.println("输入密码： "+inputPassword+"  MD5加密后： "+MD5Password);
        System.out.println("*******************************************");
         User user = new User();
          user.setPassword(password);
          user.setUsername(username);

        return userMapper.findUserByUsernameAndPassword(user);
    }

    /**
     * @Description //TODO 注册获取输入密码，使用MD%加密后存入数据库
     * @Date 8:28 下午 2022/3/14
     * @Param [user]
     * @return org.csu.mypetstore.domain.User
     **/
    public boolean register(User user){
        String inputPassword = user.getPassword();
        System.out.println("*******************************************");
        //System.out.println("输入密码： "+inputPassword+"  MD5加密后： "+MD5Password);
        System.out.println("*******************************************");

        user.setPassword(inputPassword);
        return userMapper.insertUserByUsernameAndPassword(user);
    }
    public List<User> getAllUser(){
        return userMapper.getAllUser();
    }
    public User getUser(String username){
       return userMapper.findUserByUsername(username);
    }
}
